package hexgame;



public class GameState {
	// X and Y size always equal
	int boardSize;
	boolean localPlayerTurn;
	Player localPlayer;
	Player remotePlayer;
	HexPlayer[][] board;
	
	/**
	 * Create a new GameState based upon parameters stored in a HexGameRequest
	 * @param request
	 */
	public GameState(HexGameRequest request) {
		this(request.boardSize, !request.remotePlayerFirst);

		this.remotePlayer = request.remotePlayer;
	}
	
	/**
	 * Constructor mainly exists for tests
	 * @param boardSize - size of the board squared
	 */
	public GameState(int boardSize, boolean localPlayerTurn) {
		this.boardSize = boardSize;
		this.localPlayerTurn = localPlayerTurn;
		
		board = new HexPlayer[boardSize][boardSize];
		
		for(int x=0; x<boardSize; x++) {
			for(int y=0; y<boardSize; y++) {
				board[x][y] = HexPlayer.NONE;
			}
		}
	}
	
	/**
	 * Copy-constructor for GameState
	 * @param oldState - the state to be copied
	 */
	public GameState(GameState oldState) {
		this(oldState.boardSize, oldState.localPlayerTurn);
		
		this.localPlayer = oldState.localPlayer;
		this.remotePlayer = oldState.remotePlayer;
		
		for(int x=0; x<this.boardSize; x++) {
			for(int y=0; y<this.boardSize; y++) {
				this.board[x][y] = oldState.board[x][y];
			}
		}
	}
	
	/**
	 * Gets the state of the specified hex. Must be a valid hex
	 * @param move - used to specify which square to get
	 * @return The state as a HexPlayer enum value
	 */
	public HexPlayer getHexState(HexMove move) {
		return getHexState(move.x, move.y);
	}
	
	/**
	 * Gets the state of the specified hex. Must be a valid hex
	 * @param x - x coordinate
	 * @param y - the y coordinate
	 * @return The state as a HexPlayer enum value
	 */
	public HexPlayer getHexState(int x, int y) {
		return board[x][y];
	}
	
	/**
	 * Sets the local player pointer
	 * @param local - the local player to be set
	 */
	public void setLocalPlayer(Player local) {
		this.localPlayer = local;
	}
	
	public int getBoardSize() {
		return boardSize;
	}
	
	public boolean isLocalPlayerTurn() {
		return localPlayerTurn;
	}
	
	public HexPlayer[][] getBoard() {
		return board;
	}
}
